/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel.layouts;

import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.balisunrise.annotation.PanelType;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.swing.components.panel.LayoutBuilder;

/**
 * Constroi o Layout para um painel de paineis associados para um editor 
 * de uma entidade.
 *
 * @author Glauco
 */
public class AssociatedPanelsLayoutBuilder implements LayoutBuilder{

    /**
     * Definicao do tipo do painel.
     */
    PanelType type;

    /**
     * Lista dos componentes.
     */
    List<? extends JComponent> components;

    public AssociatedPanelsLayoutBuilder(PanelType type) {
        this.type = type;
    }

    public List<? extends JComponent> getComponents() {
        return components;
    }

    public void setComponents(List<? extends JComponent> components) {
        this.components = components;
    }

    public PanelType getType() {
        return type;
    }

    public void setType(PanelType type) {
        this.type = type;
    }

    /**
     * Controi layout. Recebe uma lista de paineis que serao postos conforme
     * o tipo da definicao no painel principal.
     *
     * @param panel Painel principal onde os outros paineis serao postos.
     * @param components Lista de paineis.
     * @throws BuilderException
     */
    @Override
    public void buildLayout(JPanel panel) {

        switch(type){
            case SINGLE :
                throw new RuntimeException(
                        "Definicao do tipo do painel não pode ser SINGLE");
            case VERTICAL:
                vertical(panel, components);
                break;
            case SIDE_BY_SIDE:
                sideBySide(panel, components);
                break;
            case TABBED:
                tabbed(panel, components);
                break;
        }

    }

    /**
     * Constroi o layout com os paineis associados de modo vertical.
     * 
     * @param panel
     * @param components
     */
    private void vertical(JPanel panel, List<? extends JComponent> components){

        panel.removeAll();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        // HORIZONTAL
        GroupLayout.ParallelGroup hGroup =
                layout.createParallelGroup(GroupLayout.Alignment.LEADING, true);

        // VERTICAL
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

        for (JComponent jc : components) {
            hGroup.addComponent(jc);
            vGroup.addComponent(jc);
        }

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(hGroup));

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
            .addGroup(vGroup));
    }

    /**
     * Constroi o layout com os paineis associados de modo lado a lado.
     *
     * @param panel
     * @param components
     */
    private void sideBySide(JPanel panel, List<? extends JComponent> components){

        panel.removeAll();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        // VERTICAL
        GroupLayout.ParallelGroup vGroup =
                layout.createParallelGroup(GroupLayout.Alignment.LEADING, true);

        // HORIZONTAL
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

        for (JComponent jc : components) {
            hGroup.addComponent(jc);
            vGroup.addComponent(jc);
        }

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(hGroup));

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
            .addGroup(vGroup));
    }

    /**
     * Constroi o layout com os paineis associados em componentes de abas.
     * O Titulo de cada aba será pego da descrição do AccessibleContext do
     * componente da lista.
     *
     * @param panel
     * @param components
     */
    private void tabbed(JPanel panel, List<? extends JComponent> components)
            throws ClassCastException{

        panel.removeAll();

        JTabbedPane tabbed = new JTabbedPane();

        for (JComponent jc : components) {
            String title = jc.getAccessibleContext().getAccessibleDescription();
            tabbed.add(title, jc);
        }

        panel.add(tabbed);
    }
}


